package com.yhsoft.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.sys.controller.Response.MessageOutVo;
import com.yhsoft.sys.entity.CompanyInfo;
import com.yhsoft.sys.entity.Message;
import com.yhsoft.sys.entity.User;
import com.yhsoft.sys.mapper.MessageMapper;
import com.yhsoft.sys.service.ICompanyInfoService;
import com.yhsoft.sys.service.IMessageService;
import com.yhsoft.sys.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 通知信息表 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-27
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Resource
    private IUserService iUserService;

    @Resource
    private ICompanyInfoService iCompanyInfoService;

    @Override
    public void addSysWorkOrderNews(Integer workOrderId, Integer distributorId) {
        CompanyInfo companyInfo = iCompanyInfoService.getById(distributorId);

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getSysType, "sys");

        List<User> list = iUserService.list(wrapper);
        list.forEach(user ->{
            Message message = new Message();
            message.setTitle("您有一条新的工单需要处理");
            message.setWorkOrderId(workOrderId);
            message.setCompanyId(distributorId);
            message.setCompanyName(companyInfo.getCompanyName());
            message.setType(0);
            message.setUserId(user.getId());
            message.setReadStatus(0);
            message.setDeleted(0);
            this.getBaseMapper().insert(message);
        });
    }

    @Override
    public IPage<MessageOutVo> getList(Long pageNo, Long pageSize) {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Message::getUserId, TokenService.getUserBytoken().getId());
        // wrapper.eq(Message::getReadStatus, 0);

        IPage<Message> page = new Page<>(pageNo, pageSize);
        IPage<Message> messageIPage = this.getBaseMapper().selectPage(page, wrapper);

        IPage<MessageOutVo> convert = messageIPage.convert(message -> {
            MessageOutVo form = new MessageOutVo();
            BeanUtils.copyProperties(message, form);
            return form;
        });

        return convert;
    }

    @Override
    public String read(Integer id) {
        Message message = this.getById(id);
        if (message != null) {
            message.setReadStatus(1);
            this.updateById(message);
        }
        return "success";
    }

    @Override
    public String getNum() {
        LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Message::getUserId, TokenService.getUserBytoken().getId());
        wrapper.eq(Message::getReadStatus, 0);
        Long count = this.getBaseMapper().selectCount(wrapper);

        return count.toString();
    }

    @Override
    public void addVenderWorkOrderNews(Integer workOrderId, String venderId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getSysType, "vender");
        wrapper.eq(User::getCompanyId, venderId);

        List<User> list = iUserService.list(wrapper);
        list.forEach(user ->{
            Message message = new Message();
            message.setTitle("您有一条新的工单需要处理");
            message.setWorkOrderId(workOrderId);
            message.setType(0);
            message.setUserId(user.getId());
            message.setReadStatus(0);
            message.setDeleted(0);
            this.getBaseMapper().insert(message);
        });
    }

    @Override
    public void addSalerWorkOrderNews(Integer workOrderId, Integer distributorId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getSysType, "saler");
        wrapper.eq(User::getCompanyId, distributorId);

        List<User> list = iUserService.list(wrapper);
        list.forEach(user ->{
            Message message = new Message();
            message.setTitle("您有一条新的工单需要处理");
            message.setWorkOrderId(workOrderId);
            message.setType(0);
            message.setUserId(user.getId());
            message.setReadStatus(0);
            message.setDeleted(0);
            this.getBaseMapper().insert(message);
        });
    }
}
